03. Exercise: Map
Map Exercise
Create a Person
class
Task Description:
Let's get some practice with maps. First, let's create a Person
class (as we have done a few times in the past!).
Task Feedback:
Nice work!
Store and retrieve objects in a HashMap
Task Description:
The following steps will show you how to create a driver program to store and retrieve Person objects in a HashMap.
Task Feedback:
Nice work!
Solution
ND079 C1 L6 A03 Maps Exercise Solution
public class MapExcercise {
public static void main(String[] args) {
Map<String, Person> mapOfPeople = new HashMap<String, Person>();
Person mike = new Person("Mike", "mike@email.com");
Person shaun = new Person("Shaun", "shaun@email.com");
Person sally = new Person("Sally", "sally@email.com");
Person cesar = new Person("Cesar", "cesar@email.com");
ArrayList<Person> people = new ArrayList<Person>();
people.add(mike);
people.add(shaun);
people.add(sally);
people.add(cesar);
for (Person person : people) {
MapExcercise.addToMap(mapOfPeople, person);
}
for (String email : mapOfPeople.keySet()) {
System.out.println(email);
}
for (Person person : mapOfPeople.values()) {
System.out.println(person);
}
System.out.println("Get Mike: " + mapOfPeople.get("mike@email.com"));
System.out.println("Get Jeff: " + mapOfPeople.get("jeff@email.com"));
System.out.println("Contains Mike: " + mapOfPeople.containsKey("mike@email.com"));
System.out.println("Contains Jeff: " + mapOfPeople.containsKey("jeff@email.com"));
}
private static void addToMap(Map<String, Person> map, Person person) {
map.put(person.getEmail(), person);
}
}
public class Person {
private final String name;
private final String email;
public Person(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
return name + " " + email;
}
}